using EFJsonApp.Share.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace EFJsonApp.Controllers;

[ApiController]
[Route("api/[controller]")]
public class InterviewInfoController(AppDbContext context) : ControllerBase
{

    // POST: api/InterviewInfo
    [HttpPost]
    public async Task<IActionResult> Create([FromBody] InterviewInfo interviewInfo)
    {
        if (interviewInfo == null)
        {
            return BadRequest("InterviewInfo cannot be null.");
        }

        try
        {
            context.InterviewInfos.Add(interviewInfo);
            await context.SaveChangesAsync();
            return CreatedAtAction(nameof(Create), new { id = interviewInfo.Id }, interviewInfo);
        }
        catch (Exception ex)
        {
            return StatusCode(500, $"Internal server error: {ex.Message}");
        }
    }


    // GET: api/InterviewInfo
    [HttpGet]
    public async Task<ActionResult<IEnumerable<InterviewInfo>>> GetInterviewInfos()
    {
        return await context.InterviewInfos.ToListAsync();
    }
}
